package com.zenitera.bigdata.transform;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * map
 * 将数据流中的数据进行转换, 形成新的数据流，消费一个元素并产出一个元素
 * DataStream → DataStream
 * RichMapFunction
 */
public class Flink04_TransForm_Map_RichMapFunction {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        env
                .fromElements(1, 2, 3, 4, 5, 6, 7, 8)
                .map(new MyRichMapFunction())
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private static class MyRichMapFunction extends RichMapFunction<Integer, Integer> {

        @Override
        public Integer map(Integer value) throws Exception {
            return value * 2;
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            System.out.println("in open");
        }

        @Override
        public void close() throws Exception {
            System.out.println("in close");
        }


    }
}
